import type { SiderMenus } from "@/layout/layout";

type AccessToken = string | null;

interface AccessState {
  accessToken: AccessToken;
  isAccessCheck: boolean;
  accessMenu: SiderMenus[];
  accessRoute: string[]; // 存可访问的路由name
  userId: string;
}

export const useAccessStore = defineStore("access", {
  state: (): AccessState => {
    return {
      accessToken: null,
      isAccessCheck: false,
      accessMenu: [],
      accessRoute: [],
      userId: "",
    };
  },
  actions: {
    setAccessToken(token: AccessToken) {
      this.accessToken = token;
    },
    setAccessMenu(menu: SiderMenus[]) {
      this.accessMenu = menu;
    },
    setAccessRoute(routesName: string[]) {
      this.accessRoute = routesName;
    },
    setAccessCheck(value: boolean) {
      this.isAccessCheck = value;
    },
    setAccessUserId(id: string) {
      this.userId = id;
    },
    clearAccess() {
      this.accessToken = null;
      this.accessMenu = [];
    },
  },

  persist: {
    key: "access-token",
    pick: ["accessToken", "userId"],
  },
});
